package com.ygqh.baby.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.foxinmy.weixin4j.util.StringUtil;
import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.JobStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.AdminAbnormalLogisticsMapper;
import com.ygqh.baby.po.AdminAbnormalLogistics;
import com.ygqh.baby.po.AdminAbnormalLogisticsExample;
import com.ygqh.baby.po.AdminAbnormalLogisticsExample.Criteria;
import com.ygqh.baby.service.AdminAbnormalLogisticsService;

@Service
public class AdminAbnormalLogisticsServiceImpl implements AdminAbnormalLogisticsService {

	@Autowired
	private AdminAbnormalLogisticsMapper adminAbnormalLogisticsMapper;

	@Override
	public List<AdminAbnormalLogistics> find(JobStatus jobStatus, String logisticsCode) {
		AdminAbnormalLogisticsExample example = new AdminAbnormalLogisticsExample();
		Criteria criteria = example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		if (jobStatus != null) {
			criteria.andJobStatusEqualTo(jobStatus.name());
		}
		if (StringUtil.isNotBlank(logisticsCode)) {
			criteria.andLogisticsCodeEqualTo(logisticsCode);
		}
		return adminAbnormalLogisticsMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		AdminAbnormalLogisticsExample example = new AdminAbnormalLogisticsExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return adminAbnormalLogisticsMapper.countByExample(example);
	}

	@Override
	public AdminAbnormalLogistics findById(Long id) {
		return adminAbnormalLogisticsMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(AdminAbnormalLogistics adminAbnormalLogistics) {
		return adminAbnormalLogisticsMapper.insertSelective(adminAbnormalLogistics);
	}

	@Override
	public int update(AdminAbnormalLogistics adminAbnormalLogistics) {
		return adminAbnormalLogisticsMapper.updateByPrimaryKeySelective(adminAbnormalLogistics);
	}

	@Override
	public ResultSet<AdminAbnormalLogistics> search(QueryInfo queryInfo, String q, JobStatus jobStatus, Date startDate, Date endDate) {
		List<AdminAbnormalLogistics> list = adminAbnormalLogisticsMapper.selectSuper(queryInfo, q, jobStatus, startDate, endDate);
		int count = adminAbnormalLogisticsMapper.countSuper(q, jobStatus, startDate, endDate);
		ResultSet<AdminAbnormalLogistics> resultSet = new ResultSet<AdminAbnormalLogistics>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		AdminAbnormalLogisticsExample example = new AdminAbnormalLogisticsExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		AdminAbnormalLogistics record = new AdminAbnormalLogistics();
		record.setStatus(DataStatus.Delete);

		return adminAbnormalLogisticsMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(AdminAbnormalLogistics adminAbnormalLogistics) {
		int r = 0;
		if (adminAbnormalLogistics.getId() != null) {
			r = this.update(adminAbnormalLogistics);
		} else {
			r = this.save(adminAbnormalLogistics);
		}
		return r;
	}

}
